package com.jky.trial.service.impl;

import com.jky.trial.common.ListResult;
import com.jky.trial.common.Pagination;
import com.jky.trial.constant.DataStatus;
import com.jky.trial.entity.Log;
import com.jky.trial.entity.LogExample;
import com.jky.trial.mapper.LogMapper;
import com.jky.trial.service.LogService;
import com.jky.trial.vo.LogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class LogServiceImpl implements LogService {
    private final LogMapper recordMapper;


    @Autowired
    LogServiceImpl(LogMapper recordMapper) {
        this.recordMapper = recordMapper;
    }

    @Override
    public int create(Log record) {
        record.setCreateAt(new Date());
        return recordMapper.insertSelective(record);
    }

    public int log(Integer type, String content, Integer targetId, Integer operatorId) {
        Log log = new Log();
        log.setType(type);
        log.setRemark(content);
        log.setUserId(operatorId);
        log.setTargetId(targetId.toString());
        return recordMapper.insertSelective(log);
    }

    @Override
    public ListResult pageWithUser(LogVo record) {
        List<LogVo> list = recordMapper.pageListWithUser(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public int update(Log record) {
        LogExample recordExample = new LogExample();
        LogExample.Criteria criteria = recordExample.createCriteria();
        criteria.andIdEqualTo(record.getId());//查询条件
        return recordMapper.updateByExampleSelective(record, recordExample);
    }

    @Override
    public int del(int id) {
        return recordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Log detail(int id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<LogVo> list(LogVo record) {
        return recordMapper.pageList(record);
    }

    @Override
    public ListResult page(LogVo record) {
        List<LogVo> list = list(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public ListResult deviceRecord(LogVo record) {
        record.setType(DataStatus.LOG_DATA_TYPE_DEVICE.getVal());
        record.setDeleted(false);
        List<LogVo> list = recordMapper.deviceRecord(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public long count(Log record) {
        LogExample recordExample = new LogExample();
        LogExample.Criteria criteria = recordExample.createCriteria();
        if (record.getTargetId() != null) {
            criteria.andTargetIdEqualTo(record.getTargetId());
        }
        if (record.getType() != null) {
            criteria.andTypeEqualTo(record.getType());
        }
        criteria.andDeletedEqualTo(false);//
        return recordMapper.countByExample(recordExample);
    }
}
